using System;
using System.Collections;
using System.Web;
using System.Web.Caching;

namespace ePowerPortal_Core.Web
{
    public class AspNetCache
    {
		private static object tobj = new object();

		// Hide constructors
		private AspNetCache() { }

		public static bool CheckCache(string key)
		{
			lock (tobj) { return (HttpRuntime.Cache[key] != null); }
		}

		public static object GetCache(string key)
		{
			lock (tobj) { return HttpRuntime.Cache[key]; }
		}

		/// <summary>
		/// Set cache for an object with time alive 3 minutes
		/// </summary>
		/// <param name="key">cache key</param>
		/// <param name="val"></param>
		public static void SetCache(string key, object val)
		{
			//HttpRuntime.Cache[key] = val;
			SetCacheWithTime(key, val, 5);
		}

		/// <summary>
		/// Set cache with time alive
		/// </summary>
		/// <param name="key">Cache Key</param>
		/// <param name="val">value</param>
		/// <param name="Min">Time alive minutes</param>
		public static void SetCacheWithTime(string key, object val, double Min)
		{
			lock (tobj)
			{
				HttpRuntime.Cache.Remove(key);
				HttpRuntime.Cache.Insert(key, val, null, DateTime.Now.AddMinutes(Min), TimeSpan.Zero);
			}
		}
		/// <summary>
		/// Remove cache from Cache
		/// </summary>
		/// <param name="key">cache key</param>
		public static void RemoveCache(string key)
		{
			lock (tobj) { HttpRuntime.Cache.Remove(key); }
		}
		/// <summary>
		/// Reset all cache
		/// </summary>
		public static void Reset()
		{
			lock (tobj)
			{
				IDictionaryEnumerator ce = HttpRuntime.Cache.GetEnumerator();
				while (ce.MoveNext()) HttpRuntime.Cache.Remove(ce.Key as string);
			}
		}
		/// <summary>
		/// Remove group cache with string prefix
		/// </summary>
		/// <param name="prefix"></param>
		public static void RemoveCacheWithPrefix(string prefix)
		{
			lock (tobj) 
			{
				string key;
				IDictionaryEnumerator ce = HttpRuntime.Cache.GetEnumerator();
				while (ce.MoveNext())
				{
					key = ce.Key as string;
					if (key.StartsWith(prefix)) HttpRuntime.Cache.Remove(key);
				}
			}
		}
    }
}
